package cn.hxlc.tie.demo.modular.work.service.impl;/*
 @author rui
 @date 2021/6/21 --19:03
*/

import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.core.util.NumberUtil;
import cn.hxlc.tie.demo.modular.work.entity.WorkShop;
import cn.hxlc.tie.demo.modular.work.entity.WorkShopEntity;
import cn.hxlc.tie.demo.modular.work.mapper.WorkShopMapper;
import cn.hxlc.tie.demo.modular.work.mapper.WorkshopEntityMapper;
import cn.hxlc.tie.demo.modular.work.model.param.WorkShopEntityParam;
import cn.hxlc.tie.demo.modular.work.model.param.WorkShopParam;
import cn.hxlc.tie.demo.modular.work.model.result.WorkShopResult;
import cn.hxlc.tie.demo.modular.work.service.WorkShopEntityService;
import cn.hxlc.tie.demo.modular.work.service.WorkShopService;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;


@Service
public class WorkShopServiceImpl extends ServiceImpl<WorkShopMapper, WorkShop> implements WorkShopService {

    @Autowired
    private WorkShopMapper workShopMapper;

    @Autowired
    private WorkshopEntityMapper workshopEntityMapper;

    @Autowired
    private WorkShopEntityService workShopEntityService;

    @Override
    public PageResult<WorkShopResult> queryListPage(WorkShopParam workShopParam) {
        Page<WorkShopResult> workShopPage = PageFactory.defaultPage();
        IPage<WorkShopResult> resultPage = workShopMapper.queryListPage(workShopPage, workShopParam);
        return new PageResult<>(resultPage);
    }

    @Override
    @Transactional
    public int add(WorkShopParam workShopParam) {
        NumberUtil.checkRepeatNumber(this.baseMapper, workShopParam.getFnumber(), workShopParam.getFid());
        WorkShop workShop = new WorkShop();
        BeanUtil.copyProperties(workShopParam, workShop);
        // workShop.setFcreatetime(new Date());
        /* workShop.setFcreatorid(UserUntil.getUserid());*/
        int i = baseMapper.insert(workShop);

        List<WorkShopEntityParam> workShopEntitylist = workShopParam.getEntrys();
        for (WorkShopEntityParam wsep : workShopEntitylist) {
            wsep.setFparentid(workShop.getFid());
            workshopEntityMapper.addEntity(wsep);
        }

        return i;
    }

    @Override
    public Boolean batchDelete(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean delete(WorkShopParam workShopParam) {
        workShopEntityService.remove(new QueryWrapper<WorkShopEntity>().eq("fparentid", workShopParam.getFid()));
        return this.baseMapper.delete((Long) getKey(workShopParam));

    }

    @Override
    public WorkShopResult findByOne(Long fid) {
        WorkShopResult result = this.baseMapper.detail(fid);
        WorkShopEntityParam entry = new WorkShopEntityParam();
        entry.setFparentid(fid);
        result.setEntrys(workShopEntityService.findListBySpec(entry));
        return result;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateOne(WorkShopParam workShopParam) {
        WorkShop oldEntity = getOldEntity(workShopParam);
        WorkShop newEntity = getEntity(workShopParam);
        BeanUtil.copyProperties(oldEntity, newEntity);
        this.dealEntrys(workShopParam);
        return this.baseMapper.updateById(oldEntity)>0;

    }

    private WorkShop getOldEntity(WorkShopParam workShopParam) {
        return this.baseMapper.selectById(getKey(workShopParam));
    }

    private Serializable getKey(WorkShopParam param) {
        return param.getFid();
    }

    private WorkShop getEntity(WorkShopParam workShopParam) {
        WorkShop entity=new WorkShop();
        BeanUtil.copyProperties(entity,workShopParam);
        return entity;
    }

    @Override
    public void batch(Object[] fid, StatusEnum toEnum) {
     WorkShop info;
     for (int i=0;i<fid.length;i++){
         info=this.baseMapper.selectById(new Long(fid[i].toString()));
         if (info.getFstatus()!=null&&toEnum!=null && !info.getFstatus().equals(toEnum)){
             info.setFstatus(toEnum);
             if (toEnum.equals(StatusEnum.BASE)){
                 info.setFcreatetime(new Date());
             }else {
                 info.setFcreatetime(new Date());
             }
             this.updateById(info);
         }
     }
    }

    public void dealEntrys(WorkShopParam param) {
        if (param.getEntrys() != null) {
            workShopEntityService.remove(new QueryWrapper<WorkShopEntity>().eq("fparentid", param.getFid()));
            WorkShopEntityParam entryParam;
            for (int i = 0; i < param.getEntrys().size(); i++) {
                entryParam = param.getEntrys().get(i);
                entryParam.setFparentid(param.getFid());
                workShopEntityService.add(entryParam);
            }

        }

    }


}
